/*
rfidreader.c
提供读取RFID卡功能
designed by linxiantao

--------------------------------
2020-05-08 09:11:00
create file
--------------------------------
*/

#include "rfidreader.h"

#define RFID_READER_STATE 1001
#define CARD_READ_SIGNAL_STRENGTH 1002
#define CARD_READ_CARD_DATA_BASE 1003


uint8_t rfide_reader_port;
uint8_t rfide_reader_slave_id;

int16_t rfid_lable_val = 0xff;
///
#ifdef BATTERY_VERSION
extern int xp_wheel_set_wheel_speed(float wheela_speed,float wheelb_speed);
extern aos_mutex_t g_mutex_mag_navigation;
extern void xp_mag_move_start_or_stop(u8 flag);
#endif
///

/**
 * @brief rfid读卡设备读寄存器命令
 * 
 * @param addr 寄存器地址
 * @param read_val 读取的值地址
 * @return int16_t 返回0表示读取成功，-1表示失败。
 */
static int16_t rfid_reader_read_hold(uint16_t addr, int16_t *read_val)
{
	int32_t ret = 0;
	MB_data modbus;
	uint8_t rec_data[10];
	uint8_t rec_len;

	modbus.slave_id = rfide_reader_slave_id;
	modbus.mb_fun = mb_common_r;
	modbus.fun_code = 0x04;
	modbus.start_addr = addr;
	modbus.rwLen = 1;
	modbus.resBuf = rec_data;
	modbus.resLen = &rec_len;

	ret = xp_modbus_ask(rfide_reader_port,&modbus);
	if(ret != 0){
		return -1;
	}

	if(rec_len != 2){
		return -1;
	}

	println("read len=%d,rec_data: 0x%x,0x%x",rec_len,rec_data[0],rec_data[1]);
	*read_val = ((rec_data[0]<<8)|(rec_data[1]));
	
	return 0;
}

/**
 * @brief 获取rfid读卡设备读卡的信号强度
 * 
 * @return int16_t 0-7表示信号强度，0信号最弱，7信号最强，-1表示失败。
 */
static int16_t xp_rfid_reader_signal_strength()
{
	int32_t ret;
	int16_t card_read_signal_strength;
	
	ret = rfid_reader_read_hold(CARD_READ_SIGNAL_STRENGTH, &card_read_signal_strength);
	if(-1 == ret){
		return -1;
	}

	return card_read_signal_strength;
}

/**
 * @brief 获取rfid读卡设备状态
 * 
 * @return int16_t 0表示无卡，1表示正在读卡状态，-1表示失败。
 */
static int16_t xp_rfid_reader_get_state()
{
	int32_t ret;
	int16_t rfid_reader_state;
	
	ret = rfid_reader_read_hold(RFID_READER_STATE, &rfid_reader_state);
	if(-1 == ret){
		return -1;
	}

	return rfid_reader_state;
}

/**
 * @brief 获取rfid标签中指定字节数据
 * 
 * @param index 第几个字节
 * @return int16_t 返回rfid标签字节数据，-1表示失败。
 */
static int16_t xp_rfid_reader_get_card_data(int32_t index)
{
	int32_t ret;
	int16_t card_data;

	if(index < 0 || index > 7){
		return -1;
	}
	
	ret = rfid_reader_read_hold(CARD_READ_CARD_DATA_BASE+index, &card_data);
	if(-1 == ret){
		return -1;
	}

	return card_data;
}

/**
 * @brief 获取rfid标签值
 * 
 * @return int16_t rfid标签值
 */
int16_t xp_rfid_lable_val_get()
{
	return rfid_lable_val;
}

/**
 * @brief 复位rfid标签值
 * 
 */
void xp_rfid_lable_val_reset()
{
	rfid_lable_val = 0xff;
}

/**
 * @brief 实时检测rfid标签的值
 * 
 */
static void rfid_detect()
{
	int ret = -1;
	static u8 flag = 0;
	while(1){

		if(1 == xp_rfid_reader_get_state())
		{
			rfid_lable_val = xp_rfid_reader_get_card_data(0);
            #ifdef BATTERY_VERSION
            /* 
                这段代码是因为新的展示业务需求，2020.07.20
                检测到磁条然后沿着磁条走然后碰到rfid然后停下来
                然后弹入锁。
            */
            if(flag == 0)
            {
                aos_mutex_lock(&g_mutex_mag_navigation,AOS_WAIT_FOREVER);
                xp_mag_move_start_or_stop(0);
                aos_mutex_unlock(&g_mutex_mag_navigation);
                flag =1; 
            }
            #endif
            
            //
		}
        else
        {
            flag =0;
        }
		aos_msleep(50);
	}
}

/**
 * @brief rfid读卡器初始化
 * 
 * @param port 串口号
 * @param slave_id 从站站号
 */
void xp_rfid_reader_init(uint8_t port,uint8_t slave_id)
{	
	rfide_reader_port = port;
	rfide_reader_slave_id = slave_id;		

	
    xp_modbus_init(port, 115200, MB_PAR_NONE, 1000);

	if (0 != aos_task_new("rfid_detect", rfid_detect, NULL, 1024)) {
    	printf("create rfid detect thread fail");
    }
}

